package com.iqik.sc.ns.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.iqik.sc.ns.config.EmailControllerHandler;
import com.iqik.sc.ns.service.EmailService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Reference
    private EmailService service;

    @RequestMapping("/{email}/{code}")
    @SentinelResource(value = "sendCode", blockHandlerClass = EmailControllerHandler.class,
            blockHandler = "sendCodeBlockHandler", fallbackClass = EmailControllerHandler.class, fallback = "sendCodeFallback")
    public String sendCode(@PathVariable(value = "email") String email,
                           @PathVariable(value = "code") String code) {
        System.out.println("send code " + code + " to " + email);
        return service.sendCode(email, code) ? "true": "false";
    }

    /**
     * dubbo:
     *   scan:
     *     # dubbo 服务扫描基准包
     *     base-packages: com.iqik.sc.ns.service
     */
    @Value("${dubbo.scan.base-packages}")
    private String scanPackage;

    @RequestMapping("/exp")
    @SentinelResource(value = "getScanPackage", blockHandlerClass = EmailControllerHandler.class,
            blockHandler = "sendCodeBlockHandler", fallbackClass = EmailControllerHandler.class, fallback = "sendCodeFallback")
    public String getScanPackage() {
        try {
            Thread.sleep(2000);
        } catch (Exception e){
            e.printStackTrace();
        }
        return scanPackage;
    }

}
